<?PHP
 
class Admin_ku_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','库存管理');
		$this->assign('menuid1','6');
		$this->assign('menuid2','2');
		//权限判断
 
	//		url::msgBox('敬请期待！','-1');
		
		$db = factory::getDBO();
		$g_vegetype = $db->loadassoclist('select * from t_vegetype order by show_index  ','id');
		foreach($g_vegetype as $k=>$v){
			$g_vegetype[$k]['veges'] = $db->loadassoclist('
				select * from t_vege where type_id="'.$v['id'].'"
			');
		}
		$this->assign('g_vegetype',$g_vegetype);
	
	
	}
	
 
	##################################
	/* 库存列表  */
	##################################
	function index__(){
	    
	 	$this->assign('nav','菜品库存'); 
		$db=factory::getDBO();
		$this->assign('rs',$db->loadassoclist('select * from `t_vegetype` order by show_index asc'));	
	}
	function index_list__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))				$_POST['start']			= 0;
			if(!isset($_POST['limit']))				$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))					$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))  	$_POST['keywords']  = ''; 
  	  #查询条件
			$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name','a.plandate','a.acdate','a.remark' ));
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_ku` a  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_ku` a
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	}
	//库存批次添加
	public function  add__(){  
		//权限判断
		factory::module('admin')->authSys('ku_plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$veges = format::addslash_json(json_decode( stripslashes($_POST['veges']) ));
			 
			$db = factory::getDBO(); 
			$db->begin();
			 
			if($db->loadresult('select count(*) from t_ku where deleted=0 and name="'.$form['name'].'" ') > 0 ){
				url::msgbox('操作失败！,已经存在此库存批次标识！');
			}
			//主表
			$ku_id = $db->insert('ku',array(
				'name'=>$form['name'],
				'plandate'=>$form['plandate'],
				'created_user_id'=>factory::module('admin')->getuserid(),
				'created'=>'now()'
			));
			if(!$ku_id){
				exit('操作失败！');
			}
 			//插入包含的固定菜品
			foreach($veges as $v){
				$db->insert('ku_vege',array(
					'ku_id'=>$ku_id,
					'vege_id'=>$v['vege_id'],
					'plan_weight'=>$v['plan_weight'],
					'plan_remark'=>'',
					'cg_remark'=>'',
					'send_remark'=>'',
					'gc_remark'=>'',
					'remark'=>''
				));
			}
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		} 
	 	$this->assign('nav','库存批次 - 添加'); 
		$db=factory::getDBO();
		$this->assign('veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id')); 
	}
	/* 删除 */
	function del__(){
		//权限判断
		if(!factory::module('admin')->authSys('ku_plan',true)){
			exit('您暂无权限使用此功能！');
		}
		$_POST=format::addslash_deep($_POST);
		$db = factory::getDBO();
		$id = $_POST['id'];
		if($db->query('update `t_ku` set deleted=1 where id="'.$id.'" ')){
			exit('{success:1,msg11:"删除成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		} 
	}
	

	 
	/* 出库填写 */
	function chu__(){
		//权限判断
		factory::module('admin')->authSys('ku_chu');
		$db = factory::getDBO();
		$_GET=format::addslash_deep($_GET);
		$ku_id = intval($_GET['id']);
		$this->getKuInfo($ku_id);
	}
	//
	public function chu_save__(){  
		//权限判断
		if(!factory::module('admin')->authSys('ku_chu',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	  
			$forms_chu = format::addslash_json(json_decode( stripslashes($_POST['forms_chu']) ));
			$db = factory::getDBO();
			$db->begin();
 			// 更新t_ku_chu
			foreach($forms_chu as $v){
				$db->query('update t_ku_chu set actout_nums="'.$v['actout_nums'].'"  where ku_index_id="'.$v['ku_index_id'].'" and vege_id="'.$v['vege_id'].'" ');
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	/* 会服填写 */
	function plan__(){
		//权限判断
		factory::module('admin')->authSys('ku_plan');
		$db = factory::getDBO();
		$_GET=format::addslash_deep($_GET);
		$ku_id = intval($_GET['id']);
		$this->getKuInfo($ku_id);
	}
	//
	public function  plan_save__(){  
		//权限判断
		if(!factory::module('admin')->authSys('ku_plan',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			
			$forms = format::addslash_json(json_decode( stripslashes($_POST['forms']) ));
			$forms_vege = format::addslash_json(json_decode( stripslashes($_POST['forms_vege']) ));
			$forms_chu = format::addslash_json(json_decode( stripslashes($_POST['forms_chu']) ));
			$db = factory::getDBO();
			$db->begin();
			 
			if($db->loadresult('select count(*) from t_ku where deleted=0 and id<>"'.$forms['id'].'" and name="'.$forms['name'].'" ') > 0 ){
				exit('操作失败:已经存在此库存批次标识！');
			}
			//主表
			$db->update('ku',array(
				'id'=>$forms['id'],
				'name'=>$forms['name'],
				'plandate'=>$forms['plandate']
			));
			
 			// 更新 t_ku_vege
			foreach($forms_vege as $v){
				$db->query('update t_ku_vege set plan_weight="'.$v['plan_weight'].'",plan_remark="'.$v['plan_remark'].'" where ku_id="'.$v['ku_id'].'" and vege_id="'.$v['vege_id'].'" ');
			}
 			// 更新t_ku_chu
			foreach($forms_chu as $v){
				$db->query('update t_ku_chu set orderout_nums="'.$v['orderout_nums'].'"  where ku_index_id="'.$v['ku_index_id'].'" and vege_id="'.$v['vege_id'].'" ');
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	/* 增加订单与出库 */
	function plan_add__(){
		//权限判断
		factory::module('admin')->authSys('ku_plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$ku_id = $_POST['ku_id'];
			$aftku_index_id = $_POST['id'];
			$db = factory::getDBO(); 
			$db->begin();
			
			# 插入t_ku_index
			if($aftku_index_id == "0"){
				$now_show_index = '1';
			}else{
				$now_show_index = intval($db->loadresult('select show_index from t_ku_index where id="'.$aftku_index_id.'" ')) + 1;
			}
			$ku_index_id=$db->insert('ku_index',array(
				'ku_id'=>$ku_id,
				'type'=>'chu',
				'show_index'=>$now_show_index,
				'created'=>'now()',
				'created_user_id'=>factory::module('admin')->getuserid()
			));
			$this->KuShowIndex($ku_id);
			
			# 查找蔬菜列
			$rs_vege = $db->loadassoclist('select vege_id from t_ku_vege where ku_id="'.$ku_id.'" ');
			foreach($rs_vege as $v){
				$db->insert('ku_chu',array(
					'ku_index_id'=>$ku_index_id,
					'vege_id'=>$v['vege_id']
				));
			}
			  
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}	
	/* 删除订单与出库 */
	function plan_del__(){
		//权限判断
		if(!factory::module('admin')->authSys('ku_plan',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$id = $_POST['id'];
			if($db->query('update `t_ku_index` set deleted=1 where id="'.$id.'" ')){
				$this->KuShowIndex($db->loadresult('select ku_id from t_ku_index where id="'.$id.'" '));
				exit('{success:1}');
			}else{
				exit('系统繁忙，请稍后重试！');
			} 
		}
		exit();	
	}
	/* 计划订单导入选择条件页面 */
	function plan_ordernum_sltdate__(){
		 
	}
	/* 计划订单导入数量获取 */
	function plan_ordernum__(){

		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			
			$senddate = $_POST['senddate'];
			$sendpi = $_POST['sendpi'];
			$orderpi = $_POST['orderpi'];
			
			$db = factory::getDBO();
			
			# 计算乐园卡的
			$rs_send = array();
			if($sendpi == '1'){
				$rs_send = $db->loadassoclist('
					select 
					a.vege_id,
					sum(vege_num) as vege_num
					from `t_send_vege` a 
					left join t_sendpi b on a.sendpi_id=b.id
					where b.deleted=0 and   b.senddate="'.$senddate.'" 
					group by a.vege_id
				');
			}	
			# 计算田园卡的
			$rs_order = array();
			if($orderpi == '1'){
				$rs_goods = $db->loadassoclist('
					select 
					a.id,
					a.nums
					from `t_order_goods` a
					left  join t_order b on a.order_id=b.id  
					where b.status not in (-2,-1) and b.rec_date="'.$senddate.'" 
				');
				foreach($rs_goods as $k=>$v){
					$rs_items = $db->loadassoclist('select a.vege_id,a.vege_num*'.$v['nums'].' as vege_num  from t_order_goods_item a where order_goods_id="'.$v['id'].'" ');
					$rs_order = array_merge($rs_order,$rs_items);
				}
			}	
			$rs_vege = array_merge($rs_send,$rs_order);
			$ret = array();
			foreach($rs_vege as $v){
				if(!isset($ret[$v['vege_id']])){
					$ret[$v['vege_id']] = $v['vege_num'];
				}else{
					$ret[$v['vege_id']] += $v['vege_num'];
				}
			}
//			$rs_ret = array();
//			foreach($ret as $k=>$v){
//				$rs_ret[] = array(
//					'vege_id'=>$k,
//					'vege_num'=>$v
//				);
//			}
			exit('{success:1,results:'.json_encode($ret).'}');
		}
		exit();
	}

	 
	
	/* 库存查阅 */
	function view__(){
		//权限判断
		factory::module('admin')->authSys('ku_view');
	 
		$db = factory::getDBO();
		$_GET=format::addslash_deep($_GET);
		$ku_id = intval($_GET['id']);
		$this->getKuInfo($ku_id);
	}
	
	/* 采购填写 */
	function gou__(){
		//权限判断
		factory::module('admin')->authSys('ku_gou');
	 
		$db = factory::getDBO();
		$_GET=format::addslash_deep($_GET);
		$ku_id = intval($_GET['id']);
		$this->getKuInfo($ku_id);
	}
	public function gou_save__(){  
		//权限判断
		if(!factory::module('admin')->authSys('ku_gou',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$forms_vege = format::addslash_json(json_decode( stripslashes($_POST['forms_vege']) ));
			$db = factory::getDBO();
			$db->begin();
 			// 更新 t_ku_vege
			foreach($forms_vege as $v){
				$db->query('
				update 
				t_ku_vege 
				set 
				cg_weight="'.$v['cg_weight'].'",
				cg_price="'.$v['cg_price'].'" ,
				cg_remark="'.$v['cg_remark'].'" ,
				send_weight="'.$v['send_weight'].'" ,
				send_nums="'.$v['send_nums'].'" ,
				send_remark="'.$v['send_remark'].'" 
				where ku_id="'.$v['ku_id'].'" and vege_id="'.$v['vege_id'].'" ');
			} 
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	
	/* 工厂填写 */
	function make__(){
		//权限判断
		factory::module('admin')->authSys('ku_make');
	 
		$db = factory::getDBO();
		$_GET=format::addslash_deep($_GET);
		$ku_id = intval($_GET['id']);
		$this->getKuInfo($ku_id);
	}
	
	//
	public function make_save__(){  
		//权限判断
		if(!factory::module('admin')->authSys('ku_make',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			
			$forms = format::addslash_json(json_decode( stripslashes($_POST['forms']) ));
			$forms_vege = format::addslash_json(json_decode( stripslashes($_POST['forms_vege']) ));
			$forms_jia = format::addslash_json(json_decode( stripslashes($_POST['forms_jia']) ));
			$db = factory::getDBO();
			$db->begin();
			 
			if($db->loadresult('select count(*) from t_ku where deleted=0 and id<>"'.$forms['id'].'" and name="'.$forms['name'].'" ') > 0 ){
				exit('操作失败:已经存在此库存批次标识！');
			}
			//主表
			$db->update('ku',array(
				'id'=>$forms['id'],
				'acdate'=>$forms['acdate']
			));
 			// 更新 t_ku_vege
			foreach($forms_vege as $v){
				$db->query('update t_ku_vege set 
				gcall_weight="'.$v['gcall_weight'].'",
				gcall_nums="'.$v['gcall_nums'].'",
				gcloss_weight="'.$v['gcloss_weight'].'",
				gc_remark="'.$v['gc_remark'].'",
				fst_mateloss_weight="'.$v['fst_mateloss_weight'].'",
				fst_matemake_weight="'.$v['fst_matemake_weight'].'",
				fst_packmake_nums="'.$v['fst_packmake_nums'].'",
				bf_weight="'.$v['bf_weight'].'",
				remark="'.$v['remark'].'"
				where ku_id="'.$v['ku_id'].'" and vege_id="'.$v['vege_id'].'" ');
			}
 			// 更新t_ku_jia
			foreach($forms_jia as $v){
				$db->query('update t_ku_jia set 
				packloss_nums="'.$v['packloss_nums'].'",
				mateloss_weight="'.$v['mateloss_weight'].'",
				matemake_weight="'.$v['matemake_weight'].'",
				packmake_nums="'.$v['packmake_nums'].'"
				where ku_index_id="'.$v['ku_index_id'].'" and vege_id="'.$v['vege_id'].'" ');
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	/* 加工添加 */
	function make_add__(){
		//权限判断
		factory::module('admin')->authSys('ku_plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$ku_id = $_POST['ku_id'];
			$aftku_index_id = $_POST['id'];
			$db = factory::getDBO(); 
			$db->begin();
			
			# 插入t_ku_index
			if($aftku_index_id == "0"){
				$now_show_index = '1';
			}else{
				$now_show_index = intval($db->loadresult('select show_index from t_ku_index where id="'.$aftku_index_id.'" ')) + 1;
			}
			$ku_index_id=$db->insert('ku_index',array(
				'ku_id'=>$ku_id,
				'type'=>'jia',
				'show_index'=>$now_show_index,
				'created'=>'now()',
				'created_user_id'=>factory::module('admin')->getuserid()
			));
			$this->KuShowIndex($ku_id);
			
			# 查找蔬菜列
			$rs_vege = $db->loadassoclist('select vege_id from t_ku_vege where ku_id="'.$ku_id.'" ');
			foreach($rs_vege as $v){
				$db->insert('ku_jia',array(
					'ku_index_id'=>$ku_index_id,
					'vege_id'=>$v['vege_id']
				));
			}
			  
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}		
	/* 删除加工 */
	function make_del__(){
		//权限判断
		if(!factory::module('admin')->authSys('ku_make',true)){
			exit('您暂无权限使用此功能！');
		}
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$id = $_POST['id'];
			if($db->query('update `t_ku_index` set deleted=1 where id="'.$id.'" ')){
			//$this->KuShowIndex();
				$this->KuShowIndex($db->loadresult('select ku_id from t_ku_index where id="'.$id.'" '));
				exit('{success:1}');
			}else{
				exit('系统繁忙，请稍后重试！');
			} 
		}
	}
	
	
	
	/* 向批次添加菜品 */
	function addvege_save__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$db->begin();
			/* 
    [ku_id] => 5
    [vege_id] => 78 */
			if($db->loadresult('select count(*) from t_ku_vege where ku_id="'.$_POST['ku_id'].'" and vege_id="'.$_POST['vege_id'].'"  ') > 0 ){
				exit('操作失败:该菜品已经存在批次中！');
			}
			$db->insert('ku_vege',array(
				'ku_id'=>$_POST['ku_id'],
				'vege_id'=>$_POST['vege_id'],
				'plan_remark'=>'',
				'cg_remark'=>'',
				'send_remark'=>'',
				'gc_remark'=>'',
				'remark'=>''
			));
			#添加t_ku_index里的
			$rs_ku_index = $db->loadassoclist('select * from t_ku_index where ku_id="'.$_POST['ku_id'].'" ');
			foreach($rs_ku_index as $v){
				if($v['type'] == 'chu'){
					$db->insert('ku_chu',array(
						'ku_index_id'=>$v['id'],
						'vege_id'=>$_POST['vege_id']
					));
				}else{
					$db->insert('ku_jia',array(
						'ku_index_id'=>$v['id'],
						'vege_id'=>$_POST['vege_id']
					));
				}
			}
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	
	
	
	
	
	
	
	/* 各种填写之前必须得到的数据 */
	public function getKuInfo($ku_id){
		$db = factory::getDBO();
	  #  信息
		$rs = $db->loadassoc('select 
					a.*
				from t_ku a
				where a.id="'.$ku_id.'" and a.deleted=0 ');
		if(!$rs){
			url::msgBox('没有查到任何记录！','-1');
		}
		$rs_ku_veges = $db->loadassoclist('select 
		c.type_name as vege_typename,
		b.name as vege_name,
		b.code as vege_code,
		a.*
		 from t_ku_vege a 
		 left join t_vege b on a.vege_id=b.id
		 left join t_vegetype c on b.type_id=c.id
		 where a.ku_id="'.$ku_id.'"
		 order by c.show_index 
		  '); 
		$rs_ku_vege = $db->loadassoclist('select 
		c.type_name as vege_typename,
		b.name as vege_name,
		b.code as vege_code,
		a.*
		 from t_ku_vege a 
		 left join t_vege b on a.vege_id=b.id
		 left join t_vegetype c on b.type_id=c.id
		 where a.ku_id="'.$ku_id.'"
		 order by c.show_index 
		  ','vege_id'); 
		$rs_ku_index = $db->loadassoclist('select id,type from t_ku_index where ku_id="'.$ku_id.'" and deleted=0 order by show_index,id desc'); 
		foreach($rs_ku_index as &$d_v){
			if($d_v['type']=='chu'){
				$d_v['veges'] = $db->loadassoclist('select 
				c.type_name as vege_typename,
				b.name as vege_name,
				a.*
				 from t_ku_chu a 
				 left join t_vege b on a.vege_id=b.id
				 left join t_vegetype c on b.type_id=c.id
				 where a.ku_index_id="'.$d_v['id'].'"
				 order by c.show_index 
					','vege_id');
			}else{
				$d_v['veges'] = $db->loadassoclist('select 
				c.type_name as vege_typename,
				b.name as vege_name,
				a.*
				 from t_ku_jia a 
				 left join t_vege b on a.vege_id=b.id
				 left join t_vegetype c on b.type_id=c.id
				 where a.ku_index_id="'.$d_v['id'].'"
				 order by c.show_index 
					','vege_id');
			}
		}
		$this->assign('rs',$rs);
		$this->assign('rs_ku_vege',$rs_ku_vege);
		$this->assign('rs_ku_veges',$rs_ku_veges);
		$this->assign('rs_ku_index',$rs_ku_index);
	}	
	
	
	
	
	/* 更新加工或出库的序列号 */
	function KuShowIndex($ku_id){
		$db = factory::getDBO();
		# 更新其他的show_index
		$rs_ku_index = $db->loadassoclist('select id,show_index from t_ku_index where ku_id="'.$ku_id.'" and deleted=0 order by show_index,id desc ');
		$ii=1;
		foreach($rs_ku_index as $v){ 
			$db->update('ku_index',array(
				'id'=>$v['id'],
				'show_index'=> $ii++
			)); 
		}
	}
}
?>